<template>
  <a-input
          v-model="model"
          :placeholder="placeholder"
          :disabled="disabled"
          />
</template>
<script>
  export default {
    name: 'PriceInput',
    data () {
      return {
//        model:true,
          tModel : ''
      }
    },
    props: {
      formData:{
        type: Object|Array,
        required: true
      },
        disabled:{
        type: Boolean,
        default: false
      },
        field:{
            type: String|Number,
            required: true
        },
        placeholder:{
            type: String,
            required: false,
        }
    },
    computed:{
        model:{
            get: function () {
                if(typeof  this.formData[this.field]=='undefined'){
                    return '';
                }
                if(this.formData[this.field]===''){
                    return '';
                }
                let fieldVal = this.formData[this.field];
                let arr = this.tModel.split('.');
                console.log('arr-get',arr);
                if(arr.length==1){
                    //整数
                    if(arr[0] === '-'){
                        //负数开始
                        return '-';
                    }
                    if(arr[0] === '-0'){
                        //-0
                        return '-0';
                    }
                    return (parseInt(fieldVal)/100)
                }else{
                    //小数
                    if(arr[1] === ''){
                        //小数点结尾
                        if(arr[0] === '-0'){
                            //-0
                            return '-0.';
                        }
                        return (parseInt(fieldVal)/100) +'.';
                    }else if(arr[1] === '0'){
                        //十分位0
                        if(arr[0] === '-0'){
                            //-0
                            return '-0.0';
                        }
                        return (parseInt(fieldVal)/100) +'.0';
                    }
                    return (parseInt(fieldVal)/100)
                }
            },
            set: function (val,oldval) {
                val = val.replace(/[^\-?\d.]/g,'');
                let arr = val.split('.');
                console.log('arr-set',arr);
//                let arr2 = val.split('-');
                if(val.length>1 && val.substr(val.length-1,1) =='-'){
                    //负号只能出现在第一位
                    return ;
                }
                if(arr.length>2){
                    //不能有2个小数点
                    return;
                }
                if(arr.length==2){
                    if(arr[0]===''&&arr[1]===''){
                        //光有小数点
                        this.formData[this.field] = '0.';
                    }else if(arr[1]==='0'){
                        this.$set(this.formData,this.field,val*100)
//                        return ;
                    }else if(arr[1]===''){
                        if(arr[0]==='-'){
                            return ;
                        }
                        this.$set(this.formData,this.field,(val*100)+'.')
//                        return ;
                    }else if(arr[1].length >2 ){
                        //不能超过2位小数
                        return ;
                    }else{
                        this.$set(this.formData,this.field,val*100)
                    }
                }else{
                    if(arr[0] =='-'){
                        val = '-';
                        this.formData[this.field] = '-';
                    }if(arr[0] ===''){
                        this.formData[this.field] = '';
                    }else{
                        //整数
                        this.$set(this.formData,this.field,val*100)
                    }
                }
                this.tModel = val;
            }
        }
    },
      watch: {
//          model:val=>{
//              this.formData[this.field] = val*100;
//          }
      },
      created(){
          console.log('this.formData[this.field]',this.formData[this.field]);
//        this.model = this.formData[this.field]/100;
      },
    methods: {
    },
  }
</script>
<style>

</style>